package com.zhao.utils;

import org.springframework.util.ClassUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

public class QRTest {
    public static void main(String[] args) throws Exception {
        // 存放在二维码中的内容
        // 二维码中的内容可以是文字，可以是链接等
        //String text = "http:/10.1.188.6:8080/crud/toChat.do";
        //String text = "http:/10.1.188.6:8080/crud/";
        //String text = "http:/10.79.9.210:8080/crud/login";
        //String text = "http:/10.79.9.210:8080/crud/superviser-scan-getEbick?ebickID=燕014560&universityID=001";
        // 嵌入二维码的图片路径
        //String imgPath = "C:\\Users\\Administrator\\Pictures\\img\\dog.jpg";
        // 生成的二维码的路径及名称
        //String destPath = ClassUtils.getDefaultClassLoader().getResource("static").getPath()+"/QRCode/" + System.currentTimeMillis() + ".jpg";
        //String destPath = ClassUtils.getDefaultClassLoader().getResource("static").getPath()+"/myQRCode/" + System.currentTimeMillis() + ".jpg";
        String destPath = ClassUtils.getDefaultClassLoader().getResource("static").getPath()+"/QRCode/" + "16012001011720220512140345.jpg";
        //生成二维码
        //QRCodeUtil.encode(text, null, destPath, true);
        //System.out.println(destPath);
        // 解析二维码
        File file = new File(destPath );
        BufferedImage image = ImageIO.read(file);

        int width = image.getWidth();
        int height = image.getHeight();

        BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
        for ( int i= 0 ; i < width ; i++){
            for ( int j = 0 ; j < height; j++){
                int rgb = image.getRGB(i, j);
                grayImage.setRGB(i, j, rgb);
            }
        }

        File newFile = new File(ClassUtils.getDefaultClassLoader().getResource("static").getPath()+"/QRCode/"+ "/method1.jpg" );
        ImageIO.write(grayImage, "jpg" , newFile);

        destPath=ClassUtils.getDefaultClassLoader().getResource("static").getPath()+"/QRCode/"+ "/method1.jpg";
        String str = QRCodeUtil.decode(destPath);
        // 打印出解析出的内容
        System.out.println(str);
    }
}
